package com.rw.tool.util.expression.element;

import com.rw.tool.util.expression.exception.SignException;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * 表达式中的计算符号
 */
public class Calculation extends Sign {
    /**
     * 加号
     */
    public static final Calculation PLUS = new Calculation(0, "+");

    /**
     * 减号
     */
    public static final Calculation MINUS = new Calculation(0, "-");

    /**
     * 乘号
     */
    public static final Calculation MULTI = new Calculation(1, "×", "*");

    /**
     * 除号
     */
    public static final Calculation DIVISION = new Calculation(1,  "÷", "/");

    private static final List<Calculation> values = Arrays.asList(PLUS, MINUS, MULTI, DIVISION);

    private final int priority;

    private final List<String> sign;

    protected Calculation(int priority, String... sign) {
        this.priority = priority;
        this.sign = Arrays.asList(sign);
    }

    @Override
    public List<String> getSign() {
        return sign;
    }

    /**
     * 根据字符串解析出对应的计算符号
     */
    public static Calculation of(String sign) {
        return values.stream()
                .filter(value -> value.getSign().contains(sign))
                .findAny()
                .orElseThrow(() -> new SignException(sign));
    }

    /**
     * 获得符号的优先级
     * 优先级越高的符号越先计算
     */
    public int getPriority() {
        return priority;
    }

    /**
     * 获取符号列表
     */
    public static List<Calculation> getCalculationValues() {
        return Collections.unmodifiableList(values);
    }

    @Override
    public String toString() {
        return sign.get(0);
    }
}
